package com.example.bankend.controller;



import com.example.bankend.model.Transaction;
import com.example.bankend.model.TransferDto;
import com.example.bankend.service.impl.TransactionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@Api(tags = "交易管理")
@RestController
@RequestMapping("/api/transactions")
public class TransactionController {
    @Autowired
    private TransactionService transactionService;

    @ApiOperation(value = "新增交易记录")
    @PostMapping("/insert")
    public int insert(@RequestBody Transaction transaction) {
        return transactionService.insert(transaction);
    }

    @ApiOperation(value = "根据交易ID进行删除")
    @DeleteMapping("/delete/{tradeID}")
    public int deleteByPrimaryKey(@PathVariable Integer tradeID) {
        return transactionService.deleteByPrimaryKey(tradeID);
    }

    @ApiOperation(value = "根据交易ID进行更新")
    @PutMapping("/update/{tradeID}")
    public int updateByPrimaryKey(@RequestBody Transaction transaction) {
        return transactionService.updateByPrimaryKey(transaction);
    }

    @ApiOperation(value = "根据交易ID进行查找")
    @GetMapping("/select/{tradeID}")
    public Transaction selectByPrimaryKey(@PathVariable Integer  tradeID) {
        return transactionService.selectByPrimaryKey(tradeID);
    }

    @ApiOperation(value = "根据银行卡号进行查找")
    @GetMapping("/select1/{cardID}")
    public List<Transaction> selectByCardID(@PathVariable String cardID) {
        return transactionService.selectByCardID(cardID);
    }
    @ApiOperation(value = "实现转账功能")
    @PostMapping("/transfer") //存储过程-->转账业务
    public ResponseEntity<String> transfer(@RequestBody TransferDto transferDto) {
        System.out.println(transferDto.getCardId1());
        System.out.println(transferDto.getCardId2());
        System.out.println(transferDto.getAmount());
        String result = transactionService.transfer(transferDto);
        return ResponseEntity.ok(result);
    }
    @ApiOperation(value = "拿取所有交易信息")
    @GetMapping
    public List<Transaction> selectAll() {
        return transactionService.selectAll();
    }
}
